import { getRole, postRole, putRole } from '@/base/api/RoleApi';
import { Role } from '@/base/model/Role';
import { isEmpty } from '@/base/util/Strings';

export class FormRoleBloc {
  form = new Role();
  id = 0;

  load(id: number) {
    this.id = id;
    return getRole(id)
      .then((res) => {
        console.log(id);
        this.id = id;
        this.form = res.data;
      });
  }

  save() {
    const entity: any = {
      description: this.form.description,
      name: this.form.name,
      res: isEmpty(this.form.res) ? ['1', '3'] : this.form.res
    };
    return this.id ? putRole(this.id, entity) : postRole(entity);
  }

  reset() {
    this.form = new Role();
  }
}
